<?php
class ItemRelateForm extends Form
{
	function ItemRelateForm()
	{
		Form::Form('ItemRelateForm');
	}
	
	function on_submit()
	{
		if(User::have_permit(ADMIN_ITEM))
		{
			$relate_ids = URL::get("relate_ids");
			
			$ids 		= '';
			$item_arr 	= array();
				
			if($relate_ids!=''){
				$arr 	  	= explode(',',$relate_ids);
				
				foreach ($arr as $id){
					$item_arr[] = $id;
				}
			}
			
			DB::query("DELETE FROM item_relate WHERE item_id = ".Item::$item['id']." OR relate_id = ".Item::$item['id']);
				
			if($item_arr){
				$ids = implode(',',$item_arr);
				$re  = DB::query("SELECT id FROM item WHERE id IN($ids)");
				
				$sql_insert = '';
				if($re){
					while($product = mysql_fetch_assoc($re)){
						$sql_insert.=($sql_insert!=''?",":"")."(".Item::$item['id'].",{$product['id']}), ({$product['id']},".Item::$item['id'].")";
					}
				}
				
				if($sql_insert!='')
				{
					$sql_insert = "INSERT INTO item_relate (item_id, relate_id) VALUES $sql_insert";
					
					DB::query($sql_insert);
				}
			}
		}
		
		URL::redirect_url($_SERVER['REQUEST_URI']);
	}
	
	function draw()
	{
		if (Item::$item && isset(Item::$item['display']) && Item::$item['display'])
		{
			global $display;
			
			$re = DB::query("SELECT relate_id FROM item_relate WHERE item_id = ".Item::$item['id']);
		
			$relate_ids  = '';
			$relate_arrs = array();
			
			if($re){
				while($relate = mysql_fetch_assoc($re))
				{
					$relate_ids.=($relate_ids!=''?",":"").$relate['relate_id'];
				}
			}
			
			if($relate_ids)
			{
				$re  = DB::query("SELECT * FROM item WHERE id IN($relate_ids) AND status = 1 ORDER BY item_order");
				
				$relate_ids  = '';
				if($re){
					while($product = mysql_fetch_assoc($re)){
						$relate_ids.= ($relate_ids!=''?',':'').$product['id'];
						
						if($product['img_url'])
						{
							$product['image'] = AZLib::getImageThumb($product['img_url'],180,0,0,$product['img_server']);
						}
						else
						{
							$product['image'] = "";//"style/images/no-images-35x35.gif";
						}
						
						if(isset(CGlobal::$allCategories[$product['category_id']])){
							$product['url'] = WEB_DIR.CGlobal::$allCategories[$product['category_id']]['nice_name']."/p{$product['id']}/".AZLib::safe_title($product['name']).".html";
						}
						else{	
							$product['url'] = WEB_DIR."p{$product['id']}/".AZLib::safe_title($product['name']).".html";
						}

						$product['name']  	= stripslashes($product['name']);
						
						$product['price'] = AZLib::getPrice($product['price'],$product['currency_id']);
						
						$relate_arrs[]	  =	$product;
					}
				}
			}
			$display->add('relate_ids',$relate_ids);		
			$display->add('relate_arrs',$relate_arrs);		
			
			$display->add('item_detail',Item::$item);		
			
			$display->add("share_url", urlencode("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']));
			$display->add('WEB_NAME',WEB_NAME);		
			
			
			if(User::have_permit(ADMIN_ITEM))
			{
				$display->add('is_admin',1);
				$display->add('begin_form',$this->beginForm(false,'post',false,false,1));
				$display->add('end_form',$this->endForm(true));
			}

			$display->output("ItemRelate");
		}
		
	}
}
?>